package cn.cmxds.store.view.Impl.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.cmxds.store.R;
import cn.cmxds.store.adapter.MyHotAdapter;
import cn.cmxds.store.application.CustomApplcation;
import cn.cmxds.store.bean.HotResultsBean;
import cn.cmxds.store.presenter.IHotPresenter;
import cn.cmxds.store.presenter.Impl.HotPresenter;
import cn.cmxds.store.view.IHotView;
import cn.cmxds.store.widget.MyToolBar;
import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;

public class HotFragmentView extends BaseFragment implements IHotView {
    @BindView(R.id.toolbar)
    MyToolBar toolbar;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.wave)

    WaveSwipeRefreshLayout wave;
    Unbinder unbinder;
    private IHotPresenter prenseter;
    private Handler handler = new Handler();
    private StaggeredGridLayoutManager staggeredGridLayoutManager;
    private LinearLayoutManager layoutManager;
    private MyHotAdapter myAdapter;

    @Override
    protected void lazyLoad() {

    }
    public RecyclerView getRecycler() {
        return recycler;
    }

    public WaveSwipeRefreshLayout getWave() {
        return wave;
    }
    @Override
    public View initLayout(LayoutInflater inflater, ViewGroup container, boolean b) {
        View rootView = inflater.inflate(R.layout.fragment_hot, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
    @Override
    public void initView(){

        //设置中间小圆从白色到黑色
        wave.setColorSchemeColors(Color.WHITE, Color.BLACK);
        //设置整体的颜色
        wave.setWaveColor(Color.argb(255, 235, 79, 56));
        //下拉刷新
        wave.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       prenseter.refreshRecycleView();
                        //三秒后停止刷新
                        wave.setRefreshing(false);
                    }
                },3000);
            }
        });

        //noinspection deprecation
        recycler.setOnScrollListener(new RecyclerView.OnScrollListener(){

            private int lastPosition;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(lastPosition+1==myAdapter.getItemCount()&&newState==RecyclerView.SCROLL_STATE_IDLE){
                   prenseter.loadMoreRecycleView();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastPosition = layoutManager.findLastVisibleItemPosition();


            }
        });
        toolbar.setmTextTitleOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RecyclerView.LayoutManager layoutManager=recycler.getLayoutManager();
                if(layoutManager instanceof RecyclerView.LayoutManager){
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    linearLayoutManager.scrollToPositionWithOffset(0,0);
                    linearLayoutManager.setStackFromEnd(true);
                }
            }
        });

    }
    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        new HotPresenter(this);
        prenseter.initData();
    }
    @OnClick({R.id.toolbar_left_button,R.id.toolbar_right_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_right_button:

                break;

        }
    }
    @Override
    public void setPresenter(IHotPresenter presenter) {
        this.prenseter = presenter;
    }

    public void initRecycleView(List<HotResultsBean> hotResultsBeans) {
        if(myAdapter==null){
            layoutManager = new LinearLayoutManager(this.getActivity());
            recycler.setLayoutManager(layoutManager);
            myAdapter = new MyHotAdapter(CustomApplcation.getInstance().context,hotResultsBeans);
            recycler.setAdapter(myAdapter);

        }else {
            myAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void loadMoreRecycleView(List<HotResultsBean> hotResultsBeans) {
        myAdapter.loadMore(hotResultsBeans);
    }

    @Override
    public void refreshRecycleView(List<HotResultsBean> hotResultsBeans) {
        myAdapter.refreshMore(hotResultsBeans);

    }
}
